/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.ec.common.web.GetShareResourceServlet
   Module Description   :

   Date Created      : 2010/3/22
   Original Author   : jeff.ma
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.ec.common.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.xwork.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.FileCopyUtils;

/**
 * @author jeff.ma
 * 
 */
public class GetShareResourceServlet extends HttpServlet {

	/** logger */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	/** serialVersionUID */
	private static final long serialVersionUID = 7295564602578317929L;
	private String prePath = "/yaodian100.com/share/member";

	/** default constructor */
	public GetShareResourceServlet() {
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		if (StringUtils.isNotBlank(config.getInitParameter("prePath"))) {
			prePath = config.getInitParameter("prePath");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
	 * javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String[] files = StringUtils.split(request.getPathInfo(), "/");
		String paraFilename = files[files.length - 1];
		if (paraFilename.indexOf("?") != -1) {
			paraFilename = paraFilename.substring(0, paraFilename.indexOf("?") - 1);
		}

		String memberId = request.getParameter("memberId");

		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		// response.setContentType("text/plain"); plain/html/binary ?!

		if (StringUtils.isBlank(paraFilename) || StringUtils.isBlank(memberId)) {
			response.setStatus(HttpServletResponse.SC_NOT_FOUND);
		}
		File file = new File(prePath + File.separatorChar + memberId.substring(0, 4) + File.separatorChar
				+ memberId.substring(4, 8) + File.separatorChar + memberId + File.separatorChar + paraFilename);
		if (file.exists()) {
			logger.debug("read, file:{}, member:{}", file.toString(), memberId);
			InputStream is = new FileInputStream(file);
			OutputStream os = response.getOutputStream();
			FileCopyUtils.copy(is, os);
			os.flush();
			is.close();
			os.close();
		} else {
			logger.warn("file not found, file:{}, member:{}", file.toString(), memberId);
			response.setStatus(HttpServletResponse.SC_NOT_FOUND);
		}

	}
}
